
if (typeof global.Adebug === 'undefined') global.Adebug = {}
if (typeof global.Aconf === 'undefined') global.Aconf = {debug: 1}

Adebug.init = function () {
  Adebug.colors = require('colors')
  Adebug.colors.setTheme({
      trace: 'green',
      silly: 'rainbow',
      input: 'grey',
      gray: 'gray',
      verbose: 'cyan',
      prompt: 'red',
      info: 'green',
      data: 'blue',
      help: 'cyan',
      warn: 'yellow',
      debug: 'magenta',
      error: 'red'
  })
}
Adebug.color = function (e, args, colorFn, bg) {
  var er = e.stack.split('\n')
  er.splice(0, 2)
  var arr = []
  colorFn = colorFn || Adebug.colors.white
  for (var i = 0; i < args.length; i++) {
    if (typeof(args[i]) === 'object') arr.push(colorFn(JSON.stringify(args[i]).substr(0, 1000)))
    else arr.push(colorFn(args[i]))
  }
  arr.push(Adebug.colors.gray.underline(er.splice(0, e.n).join('\n')))
  return arr
}
Adebug.log = function () {
  if (!Aconf.debug) return
  var e = {n: 1}
  Error.captureStackTrace(e)
  var args = this.color(e, arguments)
  args.unshift('[log]: ')
  console.log.apply(console, args)
}
Adebug.warn = function () {
  if (!Aconf.debug) return
  var e = {n: 1}
  Error.captureStackTrace(e)
  var args = this.color(e, arguments, Adebug.colors.warn)
  args.unshift('[warn]: ')
  var e = Error()
  console.log.apply(console, args)
}
Adebug.info = function () {
  if (!Aconf.debug) return
  var e = {n: 1}
  Error.captureStackTrace(e)
  var args = this.color(e, arguments, Adebug.colors.info)
  args.unshift('[info]: ')
  console.info.apply(console, args)
}
Adebug.trace = function () {
  if (!Aconf.debug) return
  var e = {n: 10}
  Error.captureStackTrace(e)
  var args = this.color(e, arguments, Adebug.colors.magenta)
  args.unshift('[trace]: ')
  console.log.apply(console, args)
}
Adebug.error = function () {
  if (!Aconf.debug) return
  var e = {n: 8}
  Error.captureStackTrace(e)
  var args = this.color(e, arguments, Adebug.colors.error)
  args.unshift('[error]: ')
  console.log.apply(console, args)
}
Adebug.init()
module.exports = Adebug
